package com.vvlive.caibao.smartcity.utils;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.widget.Toast;

import java.util.Timer;
import java.util.TimerTask;

import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;

/**
 * Created by android on 2015/8/14.
 */
public class SMSSDKUtil {
    Context context;
    Throwable data;
    Handler handler;
    public static final int GET_CODE = 0x003;
    public static final int COUNT_DOWN = 0x001;

    public SMSSDKUtil(Context context, Handler handler) {
        SMSSDK.initSDK(context, "9997f26d9c62", "428d053f88f9cf68d6a3fd2b125945ad");
        this.context = context;
        this.handler = handler;
    }

    /**
     * @param phonenum 手机号
     * @param code     验证码
     *
     * 服务器提交手机跳转页面
     */

    public void submitVerificationCode(String phonenum, String code) {
        SMSSDK.submitVerificationCode("86", phonenum, code);
    }

    /**
     * MOB 回调
     */
    public void eventHandler() {
        EventHandler eventHandler = new EventHandler() {
            @Override
            public void afterEvent(int i, int i1, Object o) {
                super.afterEvent(i, i1, o);
                handler.sendMessage(handler.obtainMessage(GET_CODE, i, i1, o));
            }
        };
        //注册回调监听接口
        SMSSDK.registerEventHandler(eventHandler);
    }

    /**
     * 发送验证的码
     *
     * @param event
     * @param result 结果
     * @param obj
     */
    Class aClass;

    public void sendCode(int event, int result, Object obj, Class aClass) {

        if (SMSSDK.RESULT_COMPLETE == result) {
            if (SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE == event) {
                //在这里执行跳转页面
                    //showActivity((Activity) context, aClass);
                // Intent(aClass);
                Toast.makeText(context, "注册成功", Toast.LENGTH_SHORT).show();

            } else if (SMSSDK.EVENT_GET_VERIFICATION_CODE == event) {
                //alert("发送验证码，请稍后。。");
                Toast.makeText(context, "发送验证码，请稍后。。", Toast.LENGTH_SHORT).show();
            } else {
                (data).printStackTrace();
            }
        } else if (SMSSDK.RESULT_ERROR == result) {
            //alert("验证码错误");
        }
    }

    /**
     * 向服务器发送手机号
     *
     * @param phonenum
     */
    public void senPhone(String phonenum) {
        SMSSDK.getVerificationCode("86", phonenum);
    }


    /**
     * 跳转页面
     *
     * @param aClass
     */
    public void Intent(Class aClass) {
        Intent intent = new Intent(context, aClass);
        context.startActivity(intent);
    }

    int time = 20;
    private Timer mTimer;
    private TimerTask mTask;

    public void Countdown() {
        mTimer = new Timer();
        mTask = new TimerTask() {
            @Override
            public void run() {
                if (time > 0) {
                    handler.sendMessage(handler.obtainMessage(COUNT_DOWN, time));
                } else if (time == 0) {
                    handler.sendMessage(handler.obtainMessage(COUNT_DOWN + 1, time));
                    time = 20;
                    mTask.cancel();
                }
                time--;
            }
        };
        mTimer.schedule(mTask, 0, 1000);
    }
}
